package com.wnx.job.modules.rms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.mapstruct.ProjectMapper;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.rms.dto.ProjectParam;
import com.wnx.job.modules.rms.dto.ResumeParam;
import com.wnx.job.modules.rms.model.RmsProject;
import com.wnx.job.modules.rms.service.RmsProjectService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/rms/rmsProject")
public class RmsProjectController {
    @Resource
    private JmsStudentService studentService;
    @Resource
    private PmsResumeService resumeService;
    @Resource
    private RmsProjectService projectService;

    /**
     * 新增项目经历
     * @param projectParam
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增项目经历")
    public ResultData<String> save(@Validated @RequestBody ProjectParam projectParam,Principal principal){
        log.info("=======系统正在新增项目经历======");

        //属性复制
        RmsProject tbProject = ProjectMapper.INSTANCE.toPojo(projectParam);


        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbProject.setResumeId(resume.getId());

        projectService.save(tbProject);

        log.info("=======系统正在新增项目经历成功！======");
        return ResultData.success(Const.ADD_PROJECT_SUCCESS);

    }

    /**
     * 删除项目经历
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除项目经历")
    public ResultData<String> deleteProject(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除项目经历=====");
        //调用服务层，根据ID删除项目经历
        projectService.removeById(id);
        log.info("========系统删除项目经历成功！=====");
        return ResultData.success(Const.DELETE_PROJECT_SUCCESS);
    }


    /**
     * 查询项目经历
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("查询项目经历")
    public ResultData<RmsProject> findById(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除查询项目经历=====");
        //获取前端的项目经历ID

        //调用服务层，根据ID查询项目经历
        RmsProject project = projectService.getById(id);
        log.info("========系统查询项目经历成功！=====");
        return ResultData.success(Const.QUERY_PROJECT_SUCCESS,project);

    }


    /**
     * 修改项目经历
     * @param projectParam
     * @param principal
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改项目经历")
    public ResultData<String> update(@RequestBody ProjectParam projectParam, Principal principal){
        log.info("========系统正在修改项目经历！=====");

        //获取前端的项目经历对象

        //属性复制
        RmsProject tbProject = ProjectMapper.INSTANCE.toPojo(projectParam);



        //获取简历ID 进项目经历
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbProject.setResumeId(resume.getId());



        //调用服务层，进行修改项目经历
        projectService.updateById(tbProject);

        log.info("========系统正在修改项目经历成功！！！=====");
        //返回结果集
        return ResultData.success(Const.UPDATE_PROJECT_SUCCESS);


    }


    /**
     * 查询全部项目经历
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部项目经历")
    public ResultData<List<RmsProject>> findAll(Principal principal){
        log.info("=====系统正在查询全部项目经历=====");
        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());

        //调用服务层进行该学生项目经历
        List<RmsProject> projectList = projectService.findProjectListByResumeId(resume.getId());

        log.info("=====系统正在查询全部项目经历成功！=====");
        //返回结果集对象
        return ResultData.success(Const.QUERY_PROJECT_SUCCESS,projectList);
    }


}

